﻿Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Globalization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.Runtime

Namespace System.Workflow.Activities
    <ToolboxBitmap(GetType(WebServiceFaultActivity), "Resources.WebServiceOut.png"), ActivityValidator(GetType(WebServiceFaultValidator)), Designer(GetType(WebServiceFaultDesigner), GetType(IDesigner)), SRCategory("Standard"), DefaultEvent("SendingFault"), SRDescription("WebServiceFaultActivityDescription")> _
    Public NotInheritable Class WebServiceFaultActivity
        Inherits Activity
        Implements IPropertyValueProvider
        ' Events
        <SRCategory("Handlers"), SRDescription("OnBeforeFaultingDescr"), MergableProperty(False)> _
        Public Custom Event SendingFault As EventHandler
            AddHandler(ByVal value As EventHandler)
                MyBase.AddHandler(WebServiceFaultActivity.SendingFaultEvent, value)
            End AddHandler
            RemoveHandler(ByVal value As EventHandler)
                MyBase.RemoveHandler(WebServiceFaultActivity.SendingFaultEvent, value)
            End RemoveHandler
        End Event

        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String)
            MyBase.New(name)
        End Sub

        Protected Overrides Function Execute(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            If (executionContext Is Nothing) Then
                Throw New ArgumentNullException("executionContext")
            End If
            If (Me.Fault Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString(CultureInfo.CurrentCulture, "Error_PropertyNotSet", New Object() { WebServiceFaultActivity.FaultProperty.Name }))
            End If
            Dim service As WorkflowQueuingService = executionContext.GetService([Of] WorkflowQueuingService)
            MyBase.RaiseEvent(WebServiceFaultActivity.SendingFaultEvent, Me, EventArgs.Empty)
            Dim activityByName As WebServiceInputActivity = [TryCast](MyBase.GetActivityByName(Me.InputActivityName),WebServiceInputActivity)
            Dim queueName As IComparable = New EventQueueName(activityByName.InterfaceType, activityByName.MethodName, activityByName.QualifiedName)
            Dim message As IMethodResponseMessage = Nothing
            Dim workflowQueue As WorkflowQueue = service.GetWorkflowQueue(queueName)
            If (workflowQueue.Count <> 0) Then
                message = [TryCast](workflowQueue.Dequeue,IMethodResponseMessage)
            End If
            message.SendException(Me.Fault)
            Return ActivityExecutionStatus.Closed
        End Function

        Protected Overrides Sub Initialize(ByVal provider As IServiceProvider)
            If (MyBase.Parent Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_MustHaveParent"))
            End If
            MyBase.Initialize(provider)
        End Sub

        Private Function System.Workflow.ComponentModel.Design.IPropertyValueProvider.GetPropertyValues(ByVal context As ITypeDescriptorContext) As ICollection Implements IPropertyValueProvider.GetPropertyValues
            Dim strings As New StringCollection
            If (context.PropertyDescriptor.Name = "InputActivityName") Then
                Dim activity As Activity
                For Each activity In WebServiceActivityHelpers.GetPreceedingActivities(Me)
                    If TypeOf activity Is WebServiceInputActivity Then
                        strings.Add(activity.QualifiedName)
                    End If
                Next
            End If
            Return strings
        End Function


        ' Properties
        <SRCategory("Properties"), MergableProperty(False), Browsable(True), DefaultValue(CStr(Nothing)), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Property Fault As Exception
            Get
                Return [TryCast](MyBase.GetValue(WebServiceFaultActivity.FaultProperty),Exception)
            End Get
            Set(ByVal value As Exception)
                MyBase.SetValue(WebServiceFaultActivity.FaultProperty, value)
            End Set
        End Property

        <TypeConverter(GetType(PropertyValueProviderTypeConverter)), RefreshProperties(RefreshProperties.All), SRDescription("ReceiveActivityNameDescription"), DefaultValue(""), MergableProperty(False), SRCategory("Activity")> _
        Public Property InputActivityName As String
            Get
                Return [TryCast](MyBase.GetValue(WebServiceFaultActivity.InputActivityNameProperty),String)
            End Get
            Set(ByVal value As String)
                MyBase.SetValue(WebServiceFaultActivity.InputActivityNameProperty, value)
            End Set
        End Property


        ' Fields
        Public Shared ReadOnly FaultProperty As DependencyProperty = DependencyProperty.Register("Fault", GetType(Exception), GetType(WebServiceFaultActivity))
        Public Shared ReadOnly InputActivityNameProperty As DependencyProperty = DependencyProperty.Register("InputActivityName", GetType(String), GetType(WebServiceFaultActivity), New PropertyMetadata("", DependencyPropertyOptions.Metadata))
        Public Shared ReadOnly SendingFaultEvent As DependencyProperty = DependencyProperty.Register("SendingFault", GetType(EventHandler), GetType(WebServiceFaultActivity))
    End Class
End Namespace

